//
//  ZBLiveChannel.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/2.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBLiveChannel.h"
#import "LFLiveKit.h"
#import <PLVStreamerAPI/PLVStreamerAPI.h>

@implementation ZBLiveChannel

+ (instancetype)sharedInstance
{
    static ZBLiveChannel *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[ZBLiveChannel alloc] init];
    });
    
    return instance;
}

- (void)loginChannel:(void (^)(BOOL isSuccess , NSString *errorMsg))completion
{
    //135970   799736
    [PLVStreamerLogin loginWithChannelId:self.channelId password:self.channelPassword success:^(NSString *rtmpUrl, NSString *streamName, NSDictionary *userInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 将频道号和推流等值保存到单例中
            [ZBLiveChannel sharedInstance].rtmpUrl = rtmpUrl;
            [ZBLiveChannel sharedInstance].streamName = streamName;
            [ZBLiveChannel sharedInstance].userInfo = userInfo;
            
            if (completion) {
                completion(YES , nil);
            }
        });
    } failure:^(NSString *errName, NSString *errReason) {
        NSLog(@"login: errTitle:%@,errReason:%@",errName,errReason);
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:errName message:errReason preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion(NO , errReason);
            }
        });
    }];
}

@end
